package com.wg.mycat.separation.service.impl;

import com.wg.mycat.separation.domain.MycatInfo;
import com.wg.mycat.separation.repository.MycatInfoRepository;
import com.wg.mycat.separation.service.IMycatInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MycatInfoServiceImpl implements IMycatInfoService {

    @Autowired
    MycatInfoRepository mycatInfoRepository;

    @Override
    public List<MycatInfo> getMycatInfoList() {
        List<MycatInfo> mycatInfoList = mycatInfoRepository.findAll();
        return mycatInfoList;
    }

    @Override
    public int updateMycatInfo(String id) {
        Optional<MycatInfo> mycatInfo = mycatInfoRepository.findById(id);
        if(mycatInfo.isPresent()){
            mycatInfo.get().setName("李四");
            mycatInfoRepository.save(mycatInfo.get());
            return 1;
        }
        return 0;
    }

//    @Override
//    public int updateMycatInfo(String id) {
//        String name = "赵六";
//        int mycatInfo = mycatInfoRepository.updateMycatInfo(id,name);
//        return mycatInfo;
//    }

    @Override
    public int insertMycatInfo(int age) {
        String id = "32ad85cc6b02457f9a1942964b600716";
        String name = "王五";
        String position = "Python工程师";
        int mycatInfo = mycatInfoRepository.insertMycatInfo(id,name,age,position);
        return mycatInfo;
    }

    @Override
    public MycatInfo getMycatInfoById(String id) {
        Optional<MycatInfo> mycatInfo = mycatInfoRepository.findById(id);
        if(mycatInfo.isPresent()){
            return mycatInfo.get();
        }
        return null;
    }

    @Override
    public MycatInfo updateFromMycatInfo(String id) {
        Optional<MycatInfo> mycatInfo = mycatInfoRepository.findById(id);
        if(mycatInfo.isPresent()){
            mycatInfo.get().setName("曹琦");
            MycatInfo save = mycatInfoRepository.save(mycatInfo.get());
            return save;
        }
        return null;
    }


}
